<template>
  <div class="wrap">
    <div class="show-title">
      <span>API列表</span>
    </div>
    <!-- 搜索 -->
    <div class="searchBox">
      <el-input
        prefix-icon="el-icon-search"
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width: 30%"
        clearable
      ></el-input>
    </div>
    <!-- 列表 -->
    <el-table
      ref="table"
      :data="tableData.slice((currpage - 1) * pagesize, currpage * pagesize)"
      border
      style="width: 100%"
      stripe
      @selection-change="handleSelectionChange"
      @sort-change="changeSort"
      :row-key="getRowKeys"
    >
      <!-- <el-table-column
        type="selection"
        width="40"
        align="center"
        :reserve-selection="true"
        :key="Math.random()"
      ></el-table-column> -->
      <el-table-column
        prop="api_name"
        label="名称"
        min-width="180"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.api_name
        }}</template>
      </el-table-column>
      <el-table-column
        prop="api_desc"
        label="描述"
        min-width="200"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.api_desc
        }}</template>
      </el-table-column>
      <el-table-column
        prop="sort_name"
        label="分类"
        min-width="80"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.sort_name
        }}</template>
      </el-table-column>
      <el-table-column
        prop="need_token"
        label=" 用户"
        min-width="60"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.need_token === 1">需要登录</span>
          <span class="no_txt" v-else-if="scope.row.need_token === 0"
            >无需登录</span
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="ts_filter"
        label="时间戳"
        min-width="100"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.ts_filter === 1">校验</span>
          <span class="no_txt" v-else-if="scope.row.ts_filter === 0"
            >不校验</span
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="max_cc"
        label="并发"
        min-width="60"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{ scope.row.max_cc }}</template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        min-width="100"
        align="center"
        :key="Math.random()"
        v-if="permissionData.hideStatusArr.length < 1"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">
            <el-button
              class="upload"
              size="small"
              type="success"
              @click="frozenFn(scope.$index, scope.row)"
              v-permissions="{
                url: ['api/update/status'],
                index: scope.$index,
                key: 'hideStatus',
                id: 'dogjie',
              }"
              >冻结</el-button
            >
          </span>
          <span v-if="scope.row.status == -1">
            <el-button
              class="upload"
              size="small"
              type="warning"
              @click="activeFn(scope.$index, scope.row)"
              v-permissions="{
                url: ['api/update/status'],
                index: scope.$index,
                key: 'hideStatus',
                id: 'jihuo',
              }"
              >激活</el-button
            >
          </span>
        </template>
      </el-table-column>
      <el-table-column
        class="play-box"
        label="操作"
        min-width="100"
        align="center"
        fixed="right"
        :key="Math.random()"
        v-if="permissionData.hideActionArr.length < 1"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            @click="modifyFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['api/update'],
              index: scope.$index,
              key: 'hideAction',
              id: 'xiugai',
            }"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      right
      background
      layout="total,prev, pager, next, sizes,jumper"
      :page-sizes="pageSizesArr"
      :page-size="pagesize"
      :total="tableData.length"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>

    <!--删除全部设备  -->
    <!-- <div class="batch-btn-con">
      <el-button class="batchDelect" type="warning" plain>批量冻结API</el-button>
      <el-button class="batchDelect" type="warning" plain>批量激活API</el-button>
      <el-link
        class="tips"
        :underline="false"
        type="info"
        icon="el-icon-warning-outline"
      >冻结API：冻结API后，所有开发者及应用请求将会被拒绝。</el-link>
      <el-link
        class="tips"
        :underline="false"
        type="info"
        icon="el-icon-warning-outline"
      >激活API：激活API后，可继续提高服务。</el-link>
    </div>-->
    <!-- 点击修改，弹出框 -->
    <AllApiModify
      :dialogVisible="dialogVisible"
      :api_name_check="api_name_check"
      v-if="api_name_check !== ''"
      @refresh="fn_refresh"
    />
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import AllApiModify from "./AllApiModify";
export default {
  name: "",
  components: { AllApiModify },
  props: {},
  data() {
    return {
      currpage: 1, //分页
      pagesize: globalPageSizes[0], //初始显示，一页几个
      pageSizesArr: globalPageSizes, //globalPageSizes在public--config中全局配置，每页显示个数数组
      searchTableInfo: "", //搜索条件
      getSearchInfo: [], //搜索后，过滤后的tableData数据
      tableChecked: [], // 多选
      batchDelectId: [], //批量删除id
      checkAll: false, // 是否全选状态
      //授权，弹出框
      dialogVisible: {
        show: false,
      },
      api_name_check: "", //打开弹窗，当前行api_name
      permissionData: {
        hideActionArr: [], // 操作按钮的隐藏
        hideStatusArr: [], //激活冻结状态的隐藏
      },
    };
  },
  computed: {
    // 根据计算属性模糊搜索
    tableData() {
      const searchTableInfo = this.searchTableInfo;
      //console.log(searchTableInfo);
      if (searchTableInfo) {
        return this.getSearchInfo.filter((item) => {
          return Object.keys(item).some((key) => {
            return (
              String(item[key]).toLowerCase().indexOf(searchTableInfo) > -1
            );
          });
        });
      }
      return this.getSearchInfo;
    },
    //刷新table
    submitData() {
      return this.$store.state.submitData;
    },
  },
  watch: {
    //刷新table
    submitData(newVal, oldVal) {
      if (newVal != oldVal) {
        this.getList();
        this.currpage = 1;
      }
    },
  },
  created() {},
  mounted() {
    this.getList();
  },
  methods: {
    // 获取row的key值
    getRowKeys(row) {
      return row.api_name;
    },
    //点击 第几页
    handleCurrentChange(cpage) {
      this.currpage = cpage;
    },
    //每页有几条
    handleSizeChange(psize) {
      this.pagesize = psize;
    },
    //操作多选
    handleSelectionChange(val) {
      var that = this;
      this.batchDelectId = [];
      that.tableChecked = val;
      //  console.log(that.tableChecked);
      if (val.length > 0) {
        for (let i = 0; i < val.length; i++) {
          this.batchDelectId.push(val[i]["api_name"]);
        }
      }
    },
    //排序
    changeSort(val) {
      //   console.log(val);
    },
    //获取初始列表数据
    getList() {
      postData("/adminweb/api/list", {
        with_details: 1,
      })
        .then((res) => {
          //console.log(res);
          if (res.code == 0) {
            let apis = res.datas.apis;
            this.getSearchInfo = apis;
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //点击冻结按钮
    frozenFn(index, row) {
      var api_name = row.api_name;
      postData("/adminweb/api/update/status", {
        api_name: api_name,
        status: -1,
      })
        .then((res) => {
          console.log(res);
          if (res.code == 0) {
            // console.log(res);
            this.$message.success("已冻结");
            this.getList();
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //点击激活按钮
    activeFn(index, row) {
      var api_name = row.api_name;
      postData("/adminweb/api/update/status", {
        api_name: api_name,
        status: 0,
      })
        .then((res) => {
          console.log(res);
          if (res.code == 0) {
            // console.log(res);
            this.$message.success("已冻结");
            this.getList();
          } else {
            this.$message.error(res.code + "，" + res.msg + 3);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg + 4);
        });
    },
    //点击修改按钮，弹窗
    modifyFn(index, row) {
      this.dialogVisible = { show: true };
      this.api_name_check = row.api_name;
      //console.log(this.api_name_check);
    },
    fn_refresh() {
         this.dialogVisible = { show: false };
      this.getList();
    },
  },
};
</script>
<style scoped>
/*title*/
.show-title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  background: #c0dee2;
  padding-left: 15px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  border-radius: 12px 12px 0 0;
  font-weight: bold;
}

.show-title::before {
  content: "";
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 27px;
}

.show-title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}
/* 更改table,表头背景 */
.wrap >>> .el-table--border th,
.el-table__fixed-right-patch {
  /* background: #c0dee2; */
  background: #f0f3f4;
}
.wrap {
  margin-top: 20px;
  background-color: #fff;
  padding: 15px;
  overflow: hidden;
}
.searchBox {
  margin-bottom: 20px;
}
.wrap >>> .el-pagination {
  float: right;
  margin-top: 30px;
}

.batch-btn-con {
  background: #fff;
  margin-top: 70px;
}
.wrap >>> .tips {
  margin-top: 10px;
  display: block;
  font-size: 15px;
}
.no_txt {
  color: rgb(61, 206, 126);
}
</style>
